package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description UserController
 * @Author loe
 * @Date 2024-07-15
 */

@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户分页
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("用户分页")
    public ResponseResult<PageResponse<UserVo>> limitSearch(
            @RequestBody UserDto userDto,
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize) {
        PageResponse<UserVo> pageResponse = userService.limitSearch(userDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 用户添加
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户添加")
    @ApiImplicitParam(name = "userDto", value = "用户Dto对象", required = true, dataType = "userDto")
    @ApiOperationSupport(includeParameters = {
            "userDto.realName",
            "userDto.roleVoIds",
            "userDto.deptNo",
            "userDto.postNo",
            "userDto.dataState",
            "userDto.email",
            "userDto.mobile"
    })
    public ResponseResult insertUser(@RequestBody UserDto userDto) {
        userService.insertUser(userDto);
        return ResponseResult.success();
    }

    /**
     * 用户修改
     * @return
     */
    @PatchMapping
    @ApiOperation("用户修改")
    @ApiImplicitParam(name = "userDto", value = "用户Dto对象", required = true, dataType = "userDto")
    @ApiOperationSupport(includeParameters = {
            "userDto.realName",
            "userDto.roleVoIds",
            "userDto.deptNo",
            "userDto.postNo",
            "userDto.dataState",
            "userDto.email",
            "userDto.mobile"
    })
    public ResponseResult update(@RequestBody UserDto userDto) {
        userService.update(userDto);
        return ResponseResult.success();
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用禁用")
    public ResponseResult updateStatus(
            @PathVariable(required = true) Long id,
            @PathVariable(required = true) String status
    ) {
        userService.updateStatus(id, status);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult delete(@PathVariable String userIds) {
        userService.delete(userIds);
        return ResponseResult.success();
    }

}
